package com.zycfc.zsf.boot.util.text;

public class StringEscape
{
    private static boolean isEscapeNeededForString(final String str, final int len) {
        boolean needsHexEscape = false;
        for (int i = 0; i < len; ++i) {
            final char c = str.charAt(i);
            switch (c) {
                case '\0': {
                    needsHexEscape = true;
                    break;
                }
                case '\n': {
                    needsHexEscape = true;
                    break;
                }
                case '\r': {
                    needsHexEscape = true;
                    break;
                }
                case '\\': {
                    needsHexEscape = true;
                    break;
                }
                case '\'': {
                    needsHexEscape = true;
                    break;
                }
                case '\"': {
                    needsHexEscape = true;
                    break;
                }
                case '\u001a': {
                    needsHexEscape = true;
                    break;
                }
            }
            if (needsHexEscape) {
                break;
            }
        }
        return needsHexEscape;
    }
    
    public static String escapeString(String escapeStr) {
        if (escapeStr.matches("'(.+)'")) {
            escapeStr = escapeStr.substring(1, escapeStr.length() - 1);
        }
        String parameterAsString = escapeStr;
        final int stringLength = escapeStr.length();
        if (isEscapeNeededForString(escapeStr, stringLength)) {
            final StringBuilder buf = new StringBuilder((int)(escapeStr.length() * 1.1));
            for (int i = 0; i < stringLength; ++i) {
                final char c = escapeStr.charAt(i);
                switch (c) {
                    case '\0': {
                        buf.append('\\');
                        buf.append('0');
                        break;
                    }
                    case '\n': {
                        buf.append('\\');
                        buf.append('n');
                        break;
                    }
                    case '\r': {
                        buf.append('\\');
                        buf.append('r');
                        break;
                    }
                    case '\\': {
                        buf.append('\\');
                        buf.append('\\');
                        break;
                    }
                    case '\'': {
                        buf.append('\\');
                        buf.append('\'');
                        break;
                    }
                    case '\"': {
                        buf.append('\\');
                        buf.append('\"');
                        break;
                    }
                    case '\u001a': {
                        buf.append('\\');
                        buf.append('Z');
                        break;
                    }
                    default: {
                        buf.append(c);
                        break;
                    }
                }
            }
            parameterAsString = buf.toString();
        }
        return "'" + parameterAsString + "'";
    }
}
